package com.kitty.game.team.service;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.difushidao.service.DiFuShiDaoPKService;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.team.message.RespAroundInfo;
import com.kitty.game.team.message.RespClearnAllRequest;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.team.model.AroundInfo;
import com.kitty.game.team.model.Team;
import com.kitty.game.utils.Const;
import com.kitty.game.utils.SessionDataUtils;
import com.kitty.mina.message.MessagePusher;
import org.apache.commons.lang3.StringUtils;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class NewTeamService {
    @Autowired
    TeamService teamService;
    @Autowired
    RoleService roleService;

    public void requestJoin(Role role,int id){
        // 请求加入队伍
        int toRoleId =id;
        Team team = teamService.getTeam(toRoleId);
        if (team == null) {
            RespMsg respInviteMsg = new RespMsg();
            respInviteMsg.setMsg("队伍不存在。");
            MessagePusher.pushMessage(role, respInviteMsg);
            return;
        }
        //验证是否可以加入队伍
        if( teamService.isJoinTeam(role,id)){
            return;
        }
        if (team.getList().size() >= 5) {
            RespMsg respInviteMsg = new RespMsg();
            respInviteMsg.setMsg("队伍人数已满。");
            MessagePusher.pushMessage(role, respInviteMsg);
            return;
        }
        Team myTeam = teamService.getTeam(role.getRoleId());
        if (myTeam != null) {
            RespMsg respInviteMsg = new RespMsg();
            respInviteMsg.setMsg("你已经在队伍中。");
            MessagePusher.pushMessage(role, respInviteMsg);
            return;
        }

        Role leader = roleService.getOnlinePlayer(team.getLeaderUid());
        if (leader == null) {
            MessagePusher.pushMessage(role, new RespMsg(leader.getName() + "不在线！"));
            return;
        }
        //这里判断固定队特权
        String roleFixed = role.getFixedTeam()==null?"":role.getFixedTeam();
        String joinRoleFixed = leader.getFixedTeam();
        if(StringUtils.isNotEmpty(roleFixed) && roleFixed.equals(joinRoleFixed)){
            //如果是同一个固定队的话。 查看队员的 固定队设置 是否打开 被邀请自动通过
            Map<String, Integer> setMap = leader.getExtendBox().getSetMap();
            if(setMap.containsKey("ft_req_team")){
                Integer setMap1 = setMap.get("ft_req_team");
                if(setMap1 == 1){
                    if(agreeInviteJoinTeam(role,toRoleId,"request_join")){
                        //固定队
                        MessagePusher.pushMessage(leader, new RespMsg("你开启了固定队伍特权3，其他成员申请#R入队#n自动通过"));
                    }
                    return;
                }
            }
        }
        MessagePusher.pushMessage(leader, new RespMsg(role.getName() + "申请加入你的队伍，请打开队伍界面查看申请信息。"));
        MessagePusher.pushMessage(role, new RespMsg("你向" + leader.getName() + "申请加入队伍，请耐心等待。"));

        RespAroundInfo respAroundInfo = new RespAroundInfo();
        respAroundInfo.setInviteName(role.getName());
        ArrayList<AroundInfo> aroundInfos = new ArrayList<>();
        respAroundInfo.setList(aroundInfos);
        AroundInfo aroundInfo = new AroundInfo();
        aroundInfos.add(aroundInfo);
        aroundInfo.setIcon(role.getRoleIcon());
        respAroundInfo.setType("request_join");
        List<FiedValue> fiedValues = packTeamFied(role);
        aroundInfo.setList(fiedValues);
        MessagePusher.pushMessage(leader, respAroundInfo);

    }

    /**
     * 邀请对方加入队伍
     * @param role 邀请者
     * @param id 被邀请人id
     */
    public  void inviteJoinTeam(Role role, int id) {
        //验证是否可以邀请人员
        if( teamService.isSendInviteTeam(role,id)){
            return;
        }
        //邀请加入队伍 加入自己的队伍
        Team team = teamService.getTeam(role.getRoleId());
        if (team == null) {
            team = teamService.createTeam(role);
        }
        if (team.getList().size() >= 5) {
            RespMsg respInviteMsg = new RespMsg();
            respInviteMsg.setMsg("队伍人数已满，无法邀请新成员。");
            MessagePusher.pushMessage(role, respInviteMsg);
            return;
        }
        Role joinRole = SpringUtils.getRoleService().getOnlinePlayer(id);
        // 这里判断一下如果有固定队。并且邀请的是固定队里的成员的话。就自动通过。直接组队
        String roleFixed = role.getFixedTeam()==null?"":role.getFixedTeam();
        String joinRoleFixed = joinRole.getFixedTeam();
        if(StringUtils.isNotEmpty(roleFixed) && roleFixed.equals(joinRoleFixed)){
            //如果是同一个固定队的话。 查看队员的 固定队设置 是否打开 被邀请自动通过
            Map<String, Integer> setMap = joinRole.getExtendBox().getSetMap();
            if(setMap.containsKey("ft_inv_team")){
                Integer setMap1 = setMap.get("ft_inv_team");
                if(setMap1 == 1){
                    if( agreeInviteJoinTeam(joinRole,role.getRoleId(),"invite_join")){
                        //固定队
                        MessagePusher.pushMessage(joinRole, new RespMsg("你开启了固定队伍特权4，其他成员邀请#R入队#n自动通过"));
                    }
                    return;
                }
            }

        }
        MessagePusher.pushMessage(role, new RespMsg("你已发出邀请，请耐心等待。"));
        // 发送邀请信息到被邀请人
        RespAroundInfo respAroundInfo = new RespAroundInfo();
        respAroundInfo.setInviteName(role.getName());
        ArrayList<AroundInfo> arrayList1 = new ArrayList<>();
        respAroundInfo.setList(arrayList1);
        AroundInfo aroundInfo = new AroundInfo();
        arrayList1.add(aroundInfo);
        aroundInfo.setIcon(role.getRoleIcon());
        respAroundInfo.setType("invite_join");
        List<FiedValue> fiedValues = packTeamFied(role);
        aroundInfo.setList(fiedValues);

        MessagePusher.pushMessage(joinRole, respAroundInfo);
        MessagePusher.pushMessage(joinRole, new RespMsg(role.getName() + "邀请你加入其队伍，请打开队伍界面查看邀请信息。"));
        // 缓存邀请人名称与角色对应关系
        NutMap sessionData = SessionDataUtils.getData(joinRole);
        sessionData.setv(Const.sessionTeam + role.getName(), role.getRoleId());
    }

    /**
     * 同意加入队伍
     * @param role  当前人
     * @param id   发出邀请人
     */
    public  boolean agreeInviteJoinTeam(Role role, int id,String type) {
        //验证是否可以加入队伍
        if( teamService.isJoinTeam(role,id)){
            return false;
        }
        Team team = teamService.getTeam(id);
        if (team == null) {
            RespMsg respMsg = new RespMsg();
            respMsg.setMsg("未找到队伍信息。");
            MessagePusher.pushMessage(role, respMsg);
            return false;
        }
        Role leader = roleService.getOnlinePlayer(team.getLeaderUid());
        if (leader == null) {
            return false;
        }
        if (team.getList().size() >= 5) {
            RespMsg respMsg = new RespMsg();
            respMsg.setMsg("队伍人数已经满，无法增加队员。");
            MessagePusher.pushMessage(role, respMsg);
            return false;
        }

        Team roleTeam = teamService.getTeam(role.getRoleId());
        if (roleTeam != null) {
            RespMsg respMsg = new RespMsg();
            respMsg.setMsg("你已经有一个队伍了。");
            MessagePusher.pushMessage(role, respMsg);
            MessagePusher.pushMessage(leader, new RespMsg("#R#<" + role.getName() + "#>#n已经在别的队伍中了。"));
            return false;
        }
        team.getList().add(teamService.getMember(role));
        // 更新队员信息
        teamService.saveTeam(team);
        RespClearnAllRequest respClearnAllRequest = new RespClearnAllRequest();
        respClearnAllRequest.setType(type);
        MessagePusher.pushMessage(role, respClearnAllRequest);
        teamService.newMemberJoinTeam(team, leader, role, type);
        SpringUtils.getBean(DiFuShiDaoPKService.class).doAfterAddTeam(leader, team);
        return true;
    }

    //封装固定队信息
    public List<FiedValue> packTeamFied(Role role){
        List<FiedValue> list = new ArrayList<>();
        list.add(new FiedValue(305,4,role.getGid()));
        list.add(new FiedValue(31,2,role.getLevel()));
        list.add(new FiedValue( 1,4,role.getName()));
        list.add(new FiedValue( 29,1,role.getGender()));
        list.add(new FiedValue( 44,2,role.getPolar()));
        list.add(new FiedValue( 81,4,SpringUtils.getBean(RoleService.class).getFamilyName(role.getPolar())));
        list.add(new FiedValue( 86,7,role.getRoleIcon()));
        list.add(new FiedValue( 33,4,role.getPartyName()));
        return list;
    }

    public static void main(String[] args) {

    }
}
